/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "SceneObject.h"
#include "GameObject.h"
#include "CameraObject.h"

namespace PQClient
{
	SceneRenderProperty::SceneRenderProperty(SceneObject* obj)
		: _backgroundColor(0.5f, 0.5f, 0.5f),_ambient(0.5f,0.5f,0.5f),_fogColor(),
		_fogDensity(0.0f),_mode(Ogre::PM_SOLID),_sceneObject(obj)
	{
		assert(_sceneObject);
	}


	void SceneRenderProperty::setBackground(Ogre::ColourValue color)
	{
		_backgroundColor=color;
		_sceneObject->setBackground(_backgroundColor);
	}

	void SceneRenderProperty::setAmbient(Ogre::ColourValue color)
	{
		_ambient=color;
		_sceneObject->setAmbient(_backgroundColor);
	}

	void SceneRenderProperty::setPolyMode(Ogre::PolygonMode mode)
	{
		_mode=mode;
		_sceneObject->setPolyMode(_mode);
		
	}

	SceneObject *SceneObject::_activeScene=0;
	SceneObject::SceneObject(Ogre::RenderWindow* win)
		: Object(OBJECT_TYPE_SCENE_OBJECT),_rootObject(0),
		_window(0),_camera(0),_viewport(0),_isActivate(false),_loaded(false),
		_selectedObject(0),_sceneManager(0),_renderProperty(0),_mouseX(0),_mouseY(0)
	{
		if(!Ogre::Root::getSingletonPtr()||!win){
			//OGRE Root must created
			OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR,
					"failed to create object",
					"SceneObject::create");
		}

		_window = win;// 
		_renderProperty=new SceneRenderProperty(this);

		if(_activeScene){
			_activeScene->setIsActivate(false);
		}
		this->setIsActivate(true);
		_activeScene=this;
	}

	void SceneObject::setBackground(Ogre::ColourValue color)
	{
		if(_camera){
			_camera->setBackgroundColour(color);
		}
	}

	SceneObject::~SceneObject()
	{
		if(_renderProperty){
			delete _renderProperty;
		}
	}

	void SceneObject::activate()
	{
		if(!_loaded){
			loadScene();
			_loaded=true;
		}

		//reset the viewport that render scene
		if(_camera){
			_camera->resetViewport();
		}


		if(_activeScene){
			_activeScene->setIsActivate(false);
		}
		this->setIsActivate(true);
		_activeScene=this;
		
	}

	void SceneObject::loadScene()
	{
		_sceneManager = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC,"SceneManager"+getId());
		
		//create root game object
		_rootObject=new GameObject(this,GAME_OBJECT_ROOT);
		_rootObject->setName("RootObject");

		_camera=new CameraObject(this);
		_camera->setFarClipDistance(10000);
		_camera->setNearClipDistance(1);

		
		_sceneManager->setAmbientLight(_renderProperty->ambient());//render settings

		// Create RaySceneQuery for select object
		_raySceneQuery = _sceneManager->createRayQuery(Ogre::Ray());
		_raySceneQuery->setQueryTypeMask(Ogre::SceneManager::ENTITY_TYPE_MASK);
		_raySceneQuery->setSortByDistance(true);
		
	}

	void SceneObject::setPolyMode(Ogre::PolygonMode mode)
	{
		if(_camera){
			_camera->setPolygonMode(mode);
		}
	}

	void SceneObject::update(float deltaT)
	{
		//update CameraController
		//update UI
		//UISystem::getSingleton().update(deltaT);
		
	}

	void SceneObject::pushDebugString(const char* msg,...)
	{
	}



}
